package models;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

import net.ufrog.common.Logger;
import net.ufrog.common.utils.Strings;
import play.cache.Cache;
import play.mvc.Scope.Session;
import utils.dict.Element;

/**
 * 公众号模型
 * 
 * @author ultrafrog
 * @version 1.0, 2014-11-03
 * @since 1.0
 */
@Entity
@Table(name = "uwx_weixin")
public class Weixin extends Model {

	private static final String SESSION_WEIXIN	= "play_weixin_session_weixin";
	private static final String CACHE_PREFIX	= "play_weixin_cache_weixin_";
	
	/** 名称 */
	@Column(name = "vc_name")
	public String name;
	
	/** 原始微信号 */
	@Column(name = "vc_original_code")
	public String originalCode;
	
	/** 微信号 */
	@Column(name = "vc_code")
	public String code;
	
	/** 图标 */
	@Column(name = "vc_logo")
	public String logo;
	
	/** 应用编号 */
	@Column(name = "vc_app_id")
	public String appId;
	
	/** 应用密钥 */
	@Column(name = "vc_app_secret")
	public String appSecret;
	
	/** 消息密钥 */
	@Column(name = "vc_aes_key")
	public String aesKey;
	
	/** 状态 */
	@Column(name = "dc_status")
	public String status;
	
	/** 类型 */
	@Column(name = "dc_type")
	public String type;
	
	/**
	 * 通过编号读取公众号<br>
	 * 优先从缓存读取数据
	 * 
	 * @param id
	 * @return
	 */
	public static Weixin getById(String id) {
		Weixin weixin = Cache.get(CACHE_PREFIX + id, Weixin.class);
		if (weixin == null) {
			weixin = Weixin.findById(id);
			Cache.add(CACHE_PREFIX + id, weixin);
			Logger.info("cache weixin by id: %s", id);
		}
		return weixin;
	}
	
	/**
	 * 清除缓存
	 * 
	 * @param id
	 */
	public static void clearById(String id) {
		Cache.delete(CACHE_PREFIX + id);
		Logger.info("delete weixin cache by id: %s", id);
	}
	
	/**
	 * 设置当前公众号编号
	 * 
	 * @param id
	 * @return
	 */
	public static String currentId(String id) {
		Session.current().put(SESSION_WEIXIN, id);
		return id;
	}
	
	/**
	 * 读取当前公众号编号
	 * 
	 * @return
	 */
	public static String currentId() {
		try {
			return Session.current().get(SESSION_WEIXIN);
		} catch (Exception e) {
			Logger.warn("cannot get current weixin id, for reason: %s", e.getMessage());
			return null;
		}
	}
	
	/**
	 * 读取当前公众号
	 * 
	 * @return
	 */
	public static Weixin current() {
		if (Strings.empty(currentId())) {
			Weixin weixin = Weixin.find("byStatus", Status.CURRENT).first();
			if (weixin != null) currentId(weixin.id);
			else return null;
		}
		return getById(currentId());
	}
	
	/**
	 * 状态
	 * 
	 * @author ultrafrog
	 * @version 1.0, 2014-11-03
	 * @since 1.0
	 */
	public static class Status {
		
		@Element("正常")
		public static final String ENDABLED	= "01";
		
		@Element("当前")
		public static final String CURRENT	= "10";
		
		@Element("失效")
		public static final String DISABLED	= "99";
	}
	
	/**
	 * 类型
	 * 
	 * @author ultrafrog
	 * @version 1.0, 2014-11-03
	 * @since 1.0
	 */
	public static class Type {
		
		@Element("订阅号")
		public static final String SUBSCRIBE	= "01";
		
		@Element("服务号")
		public static final String SERVER		= "02";
	}
}
